package com.sb.framework.exception;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;

import com.sb.framework.bean.ErrorCode;
import com.sb.framework.event.SystemErrorEvent;

public class SystemErrorException extends BusinessException {

	private static final Logger	logger				= Logger.getLogger( SystemErrorException.class );

	private static final long	serialVersionUID	= 633709110102679821L;

	public SystemErrorException(ErrorCode code) {
		super( code );
		try {
			SystemErrorEventPublisher.publishEvent( new SystemErrorEvent( this ) );
		} catch (Exception e) {
			logger.error( "SystemErrorEvent publish failure...." );
		}
	}

	public SystemErrorException(ErrorCode code, String message) {
		super( code, message );
		try {
			SystemErrorEventPublisher.publishEvent( new SystemErrorEvent( this ) );
		} catch (Exception e) {
			logger.error( "SystemErrorEvent publish failure...." );
		}
	}

	public static final class SystemErrorEventPublisher implements ApplicationEventPublisherAware {

		private static ApplicationEventPublisher systemErrorEventPublisher = null;

		@Override
		public void setApplicationEventPublisher(ApplicationEventPublisher paramApplicationEventPublisher) {
			systemErrorEventPublisher = paramApplicationEventPublisher;
		}

		public static final void publishEvent(ApplicationEvent event) {
			systemErrorEventPublisher.publishEvent( event );
		}

	}

}
